package cn.edu.scau.cmi.xiaowenyang.client;

import cn.edu.scau.cmi.xiaowenyang.abstractFactoryMethod.AbstractFactory;
import cn.edu.scau.cmi.xiaowenyang.abstractFactoryMethod.PICCFactory;
import cn.edu.scau.cmi.xiaowenyang.abstractFactoryMethod.PingAnFactory;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class AbstractFactoryMethodClient extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	String type = new String("司机受伤");
	String company = new String("中国人寿保险");

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("抽象工厂方法");
		Pane pane = new Pane();
		Button button = new Button("显示保险信息");
		TextArea textArea = new TextArea();
		Label label1 = new Label("姓名 ：肖文洋     学号：201627020121");
		Label label2 = new Label("请选择保险");
		Label label3 = new Label("请选择保险公司");
		ImageView imageView = new ImageView();
		ComboBox<String> comboBox = new ComboBox<>();
		comboBox.setEditable(true);
		comboBox.getItems().addAll("司机受伤", "汽车损坏", "人员伤亡");
		comboBox.getSelectionModel().select(0);
		ComboBox<String> comboBox1 = new ComboBox<>();
		comboBox1.setEditable(true);
		comboBox1.getItems().addAll("中国人寿保险", "平安保险");
		comboBox1.getSelectionModel().select(0);

		comboBox.valueProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				// TODO Auto-generated method stub
				type = newValue;
			}
		});

		comboBox1.valueProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				// TODO Auto-generated method stub
				company = newValue;
			}
		});

		button.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// TODO Auto-generated method stub
				AbstractFactory piccFactory = new PICCFactory();
				AbstractFactory pinganFactory = new PingAnFactory();
				AbstractFactory factory = null;
				switch (company) {
				case "中国人寿保险":
					factory = piccFactory;
					break;
				case "平安保险":
					factory = pinganFactory;
					break;
				}
				String str = "这是" + company + "的";
				switch (type) {
				case "司机受伤":
					str += factory.getDriverInjuryInsurance().display();
					break;
				case "汽车损坏":
					str += factory.getCarBrokenInsurance().display();
					break;
				case "人员伤亡":
					str += factory.getCasualtyInsurance().display();
					break;
				}
				str += "的保险信息";
				textArea.setText(str);
			}

		});

		imageView.setImage(new Image("natsuiromatsuri.jpg", 300, 0, true, true));
		imageView.setLayoutX(0);
		imageView.setLayoutX(10);

		label1.setFont(new Font(20));
		label1.setLayoutX(350);
		label1.setLayoutY(10);

		label2.setFont(new Font(15));
		label2.setLayoutX(350);
		label2.setLayoutY(150);

		label3.setFont(new Font(15));
		label3.setLayoutX(330);
		label3.setLayoutY(100);

		comboBox.setLayoutX(450);
		comboBox.setLayoutY(150);

		comboBox1.setLayoutX(450);
		comboBox1.setLayoutY(100);

		button.setLayoutX(500);
		button.setLayoutY(200);

		textArea.setPrefHeight(400);
		textArea.setPrefWidth(1000);
		textArea.setLayoutX(0);
		textArea.setLayoutY(300);
		textArea.setEditable(false);

		pane.getChildren().addAll(textArea, button, label1, label2, imageView, comboBox, comboBox1, label3);

		primaryStage.setScene(new Scene(pane, 1000, 700));
		primaryStage.show();
	}

}
